package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.UUIDSlicePredicate;
import net.ib.mntalk.protocol.Common.ViewChange;
import net.ib.mntalk.protocol.Session.GetViewPropertiesReq;
import net.ib.mntalk.protocol.Session.GetViewsChangesReq;
import net.ib.mntalk.protocol.Session.GetViewsChangesRes;
import net.ib.mntalk.protocol.Session.GetViewsReq;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.task.DBDeleteViewTask;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class GetViewsChangesTask extends AsyncNetTask {

	private GetViewsChangesReq mGetViewsChangesReq;
	
	public GetViewsChangesTask(Context context, Handler handler, GetViewsChangesReq getViewsChangesReq){
		super(context, handler);
		this.mGetViewsChangesReq = getViewsChangesReq;
	}
	
	@Override
	protected TcpEnvelope onPreExecute() {
		
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mGetViewsChangesReq)
			.setMethodId(Session.View.GetViewsChanges)
			.build();
		return rev;
	}
	
	@Override
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("GetViewsChangesTask.onPostExecute() resultCode : " + resultCode);
		if(receivedObject == null){
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetViewsChangesRes getViewsChangesRes = (GetViewsChangesRes) resultEnvelope.getMessage();
		
		if(getViewsChangesRes.getResponseCode() == Common.SUCCESS){
			// TODO : 	
			for(int i = 0; i < getViewsChangesRes.getViewChangeCount(); i++){
				ViewChange viewChange = getViewsChangesRes.getViewChange(i);
				viewChange.getNotiUuid();
				
				if(viewChange.getType() == Common.ADDED){
					// GetView 를 호출해서 뷰 정보를 받아 온다.
					GetViewsReq getViewsReq = new GetViewsReq();
					UUIDSlicePredicate uuidSlicePredicate = new UUIDSlicePredicate();
					uuidSlicePredicate.addId(viewChange.getViewUuid());
					getViewsReq.setPredicate(uuidSlicePredicate);
					
					try{
						GetViewsTask task = new GetViewsTask(mContext, mUiHandler, getViewsReq);
						task.execute();
					} catch(Exception e){
						MTalkLogWriter.e(e);
					}
					
				}else if(viewChange.getType() == Common.REMOVED){
					// 클라이언트처리!! 그냥 지운다!!.
					try{
						DBDeleteViewTask task = new DBDeleteViewTask(mContext, mUiHandler, viewChange.getViewUuid());
						task.execute();
					} catch(Exception e){
						MTalkLogWriter.e(e);
					}
				}else if(viewChange.getType() == Common.MODIFIED){
					// 바뀐것을 다시 받아와야 한다.
					GetViewPropertiesReq getViewPropertiesReq = new GetViewPropertiesReq();
					getViewPropertiesReq.setViewUuid(viewChange.getViewUuid());
					for(int j = 0; j < viewChange.getPropertyCount(); j++){
						switch(viewChange.getProperty(j)){
							case Common.TITLE:
								getViewPropertiesReq.addProperty(Common.TITLE);
								break;
							case Common.LATEST_MESSAGE:
								getViewPropertiesReq.addProperty(Common.LATEST_MESSAGE);
								break;
							case Common.MEMBER_COUNT:
								getViewPropertiesReq.addProperty(Common.MEMBER_COUNT);
								break;
							case Common.UNREAD_COUNT:
								getViewPropertiesReq.addProperty(Common.UNREAD_COUNT);
								break;
						}
					}
					
					try{
						GetViewPropertiesTask task = new GetViewPropertiesTask(mContext, mUiHandler, getViewPropertiesReq);
						task.execute();
					} catch(Exception e){
						MTalkLogWriter.e(e);
					}
					
				}
			}
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_VIEWS_CHANGES_SUCCESS));
		} else{
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_VIEWS_CHANGES_FAIL));
		}
		
	}
}
